<?php
class SmsManageAction extends BaseAction {
	
	/** 系统配置 - 短信配置 **/
	public function sms(){
		$sms = model('Xdata')->lget('sms');
		//dump($sms);
		$this->assign('sms',$sms);
		$this->display();
	}
	
	/** 系统配置 - 短信配置 **/
	public function saveSms(){
		if($_POST){
			unset($_POST['__hash__']);
			$provider = $_POST['provider'];
			unset($_POST['provider']);
			$datastr = serialize($_POST);
			//echo $datastr;
			$data[$provider]=$datastr;
			model('Xdata')->lput('sms',$data);
			$this->assign('jumpUrl', U('/Global/sms'));
			$this->success('保存成功');
		}else{
			$provider = $_GET['provider'];
			if(!empty($provider)){
				$sms = model('Xdata')->get('sms:'.$provider);
				$this->assign('provider',$provider);
				$this->assign(unserialize($sms));
			}
			
			$this->display('editSms');
		}
	}
	
	/** 系统配置 - 短信配置 **/
	public function deleteSms(){
		$provider = $_GET['provider'];
		//echo $provider;
		if(!empty($provider))
			model('Xdata')->delete('sms:'.$provider);
		$this->assign('jumpUrl', U('/Global/sms'));
		$this->success('删除成功');
	}
	
	/**
	 * 调用短信接口相关函数
	 */
	public function callFunction(){
		$callfun 	= $_REQUEST['callfun'];//要调用的功能代码
		$provider 	= $_REQUEST['provider'];//提供商代码
		$senderType	= $_REQUEST['senderType'];//类型：sms或mms
		//$sender = $this->getSender($provider);
		//echo "121212";
		//exit;
		//if(empty($sender)){
		//	$result['status']=-1;
		//	$result['msg']='获取提供商接口对象出错！';
		//	exit(json_encode($result));
		//}
		$result['status']=-1;
		$result['msg']='对应接口不存在('.$callfun.')！';
		$senderService = service("Sender");
		if($senderType=='sms'){
			if($callfun=='logout'){
				$result = $senderService->logout4Sms();
			}
			if($callfun=='login'){
				$result = $senderService->login4Sms();
			}
			if($callfun=='getbalance'){
				$result = $senderService->getBalance4Sms();
			}
			if($callfun=='test'){
				$mobile = $_REQUEST['mobile'];
				$content = $_REQUEST['content'];
				$center = service("SendCenter");
				$result = $center->sendSms(array($mobile),$content);//sendSms(array($mobile),$content);
			}
			if($callfun=='updpw'){
				$newPassword = $_REQUEST['newPassword'];
				
				if(!empty($newPassword)){
					$result = $senderService->updatePassword($newPassword);
				}else{
					$result['status']=-1;
					$result['msg']='录入新密码！';
				}
			}
		}
		if($senderType=='mms'){
			if($callfun=='getbalance'){
				$result = $senderService->getBalance4Mms();
			}
			if($callfun=='test'){
				$title = $_REQUEST['title'];
				$mobile = $_REQUEST['mobile'];
				$content = $_REQUEST['content'];
				$center = service("SendCenter");
				$cardfpath = "system/mmstestimg.jpg";
				$result = $center->sendMms(array($mobile),$content,$title,$cardfpath);//sendMms($title,array($mobile),$content);//sendSms(array($mobile),$content);
			}
		}
		exit(json_encode($result));
	}
	
	private function getSender($provider,$senderType='sms'){
		//$provider = 'emay';
		$smsConfig = model('Xdata')->lget('sms');
		$config = unserialize($smsConfig[$provider]);
		if(empty($config)){
			return null;
		}
		define('SCRIPT_ROOT',  SMS_LIB_PATH.'/'.$provider.'/');
		require_once SCRIPT_ROOT.'include/SmsClient.php';
		require_once SCRIPT_ROOT.'include/MmsClient.php';
		
	}
}